<?php

namespace app\common\business;

use app\common\model\mysql\Goods as GoodsModel;
use app\common\business\GoodsSku as GoodsSkuBus;
use think\facade\Log;

class Goods extends BusBase
{

    public $model = null;

    public function __construct()
    {
        $this->model = new GoodsModel();
    }

    public function add($data)
    {
        if (empty($data) || !is_array($data)) {
            return 0;
        }

        $data['status'] = GoodsModel::STATUS_NORMAL;
        try {
            $this->model->save($data);
        } catch (\Exception $e) {
            Log::error(json($e->getMessage()));
            return 0;
        }
        return $this->model->id;
    }

    public function insertData($data)
    {
        $this->model->startTrans();
        try {
            $goodsId = $this->add($data);

            if (!$goodsId) {
                return $goodsId;
            }

            if ($data['goods_specs_type'] == GoodsModel::GOODS_SPECS_TYPE_UNITE) {

                //TODO 统一规格处理
                return true;
            } elseif ($data['goods_specs_type'] == GoodsModel::GOODS_SPECS_TYPE_MULTI) {
                //多规格处理
                $goodsSkuBus = new GoodsSkuBus();
                $data['goods_id'] = $goodsId;

                $res = $goodsSkuBus->saveAll($data);

                if (!empty($res)) {
                    $stock = array_sum(array_column($res, 'stock'));

                    //方案一：将sku表里的其中一条回写到goods表里。后续方案使用消息队列更新goods里的商品信息
                    $goodsUpdateData = [
                        'price' => $res[0]['price'],
                        'cost_price' => $res[0]['cost_price'],
                        'stock' => $stock,
                        'sku_id' => $res[0]['id'],
                    ];

                    $goodsRes = $this->model->updateById($goodsId, $goodsUpdateData);

                    if (!$goodsRes) {
                        throw new \think\Exception('sku:goods表回写失败');
                    }
                } else {
                    throw new \think\Exception('sku表新增失败');
                }
            }
            $this->model->commit();
            return true;
        } catch (\think\Exception $e) {
            $this->model->rollback();
        }
        return false;
    }

    public function getLists($data, $limit = 5)
    {

        $likeKeys = [];
        if (!empty($data)) {
            $likeKeys = array_keys($data);
        }

        try{
            $list = $this->model->getLists($likeKeys,$data, $limit);
            $result = $list->toArray();
        } catch (\Exception $e) {
            $result = [
                'total' => 0,
                'per_page' => $limit,
                'current_page' => 1,
                'last_age' => 0,
                'data' => [],
            ];
        }

        return $result;
    }


    public function recommend($data)
    {
        if(!isset($data['id']) || !isset($data['is_index_recommend'])) {
            return false;
        }

        $res = $this->getById($data['id']);
        if (!$res) {
            throw new \think\Exception('不存在该记录');
        }

        if ($res['is_index_recommend'] == $data['is_index_recommend']) {
            throw new \think\Exception('两次状态修改相同');
        }

        $updateData = [
            'is_index_recommend' => intval($data['is_index_recommend']),
        ];

        try {
            $res = $this->model->updateById($data['id'], $updateData);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    }
}
